<template>
  <div>
    <view class="top" @touchend="cancel">
     <view v-if="isRecording" class="sounds" >
       <view class="img">
         <view class="img-left"></view>
         <view class="img-right"></view>
       </view>
       <view class="font-bottom">手指往上滑 取消生成</view>
     </view>
    </view>
    <view class="bottom" @touchend="cancel">
      <view class="title">不知道发什么好？ 跟我念</view>
      <view style="margin-bottom: 20rpx;">1  我要发财，发财发财发财发财</view>
      <view>
        1   从前有座山，山上有座庙，庙里有个老和尚，老和尚睡着了。从前有座山，山上有座庙，庙里有个老和尚，老和尚睡着了。
      </view>
    </view>
    <view class='footer'>
      <button class="record_btn" type="default" size="default" @touchstart="play" @touchmove="touchMove" @touchcancel="cancel" @touchend="end">
        <view class="left_pic">
          <image src="/static/images/luyin Copy@3x.png.png" class="icon_record"></image>
        </view>
        <view class="right_font" >按住说话</view>
      </button>
    </view>
  </div>
</template>

<script>

    export default {

      data(){
        return {
          isRecording: false,
        }
      },

      methods: {
        play: function () {
          this.recorderManager = wx.getRecorderManager();
          const options = {
            duration: 10000,//指定录音的时长，单位 ms
          }
          const start = new Date().getTime();
          this.recorderManager.onStart(() => {
            console.log('recorder start')
          });
          this.isCancel = false;
          this.isRecording = true;
          this.recorderManager.start(options);

          //错误回调
          this.recorderManager.onError((res) => {
            this.isRecording = false;
            console.log(res);
          });
          //停止回调
          this.recorderManager.onStop((res)=>{
            this.isRecording = false;
            if(this.isCancel) {
              console.log('已经取消')
              return;
            }
            if(new Date().getTime() - start < 2000) {
              console.log('时间太短')
              return;
            }
            console.log('已经完成')
            this.src=res.tempFilePath
            console.log(res.tempFilePath )
          });
        },
        end: function (e) {
          //todo 完成
          console.log(e.mp);
          console.log('原地end')
          this.recorderManager.stop();
        },
        cancel: function () {
          this.isCancel = true;
        },
        touchMove: function (e) {
          console.log(e.mp)
        }
      }
    }
</script>

<style scoped>
.top{
  width:100%;
  height:668rpx;
  background: #eee;
  padding-top: 314rpx;
  box-sizing: border-box;
}
.sounds{
  width:400rpx;
  height:320rpx;
  margin-left: 176rpx;
  background: rgba(255,255,255,0.50);
  box-shadow: 0 2rpx 10rpx 0 rgba(255,255,255,0.50);
  border-radius:8px;
}
.img{
  width:100%;
  height:262rpx;
}
.img-left{
  width:138rpx;
  height:138rpx;
  background:pink;
  margin-top:50rpx;
  margin-left: 90rpx;
  box-sizing: border-box;
  float: left;
  font-size: 28rpx;
  color: #333333;

}

.img-right{
  width:60rpx;
  height:138rpx;
  margin-left: 22rpx;
  float: left;
  margin-top:50rpx;
  margin-right: 90rpx;
  background: skyblue;


}
.font-bottom{
  width:100%;
  height:58rpx;
  font-size: 28rpx;
  color: #333333;
  text-align: center;
  padding: 6rpx 0  0 12rpx;
  box-sizing: border-box;
  border-top: 1rpx solid #eee;

}

  .bottom{
    width:100%;
    height:440rpx;
    padding: 0 20rpx 0 20rpx;
    box-sizing: border-box;
    /*background: pink;*/
  text-align: left;
    font-size: 28rpx;
    color: #333333;

  }
.title{
  text-align: center;
  padding:40rpx 0 20rpx 0;
  box-sizing: border-box;

}
.footer{
  width:100%;
  height:98rpx;
  background: yellow;
}
/*底部固定区  */

.footer {
  width: 100%;
  height: 98rpx;
  position: fixed;
  z-index: 12;
  bottom: 0;
  background: #eee;
  padding: 18rpx 158rpx 16rpx 160rpx;
  box-sizing: border-box;
}

.footer .record_btn {
  width:432rpx;
  background: #fff;
  border: 0 solid #666;
  border-radius: 8rpx;
  height: 64rpx;
  line-height: 64rpx;
  font-size: 24rpx;
}

.record_btn::after {
  line-height: 64rpx;
}

.icon_record {
  width: 48rpx;
  height: 48rpx;
  margin-top: 8rpx;
  padding-right: 20rpx;
}

.left_pic {
  float: left;
  margin-left: 90rpx;
}

.right_font {
  float: left;
  font-size: 24rpx;
  color: #666;
}

</style>
